package com.sfdk.pools
{
    import com.sfdk.graphics.core.BitmapEx;

    import flash.display.BitmapData;
    import flash.utils.Dictionary;

    public class BitmapExPool
    {
        private static var instance_:BitmapExPool;

        private var _pool:Dictionary;

        public function BitmapExPool() {
            _pool = new Dictionary();
            instance_ = this;
        }

        public static function get instance():BitmapExPool {
            return instance_;
        }

        /**
         * Ajoute les spritesheet à la map
         * Renvoi :
         *             -1 si la spritesheet existe deja
         *             1 si tout est ok
         **/
        public function addToPool(tilesetImage:BitmapData, name:String):int {
            if (_pool[name] != null)
                return -1;

            _pool[name] = tilesetImage;
            return 1;
        }

        public function removeFromPool(name:String):void {
            _pool[name] = null;
        }

        public function getFromPool(name:String, baseDirectory : String = null):BitmapEx {
            var bitmapEx:BitmapEx = _pool[name] as BitmapEx;
            if (bitmapEx == null) {
                bitmapEx = new BitmapEx(baseDirectory, name);
                _pool[name] = bitmapEx;
            }
            return bitmapEx;
        }


        public function isInPool(name:String):Boolean {
            return (_pool[name] != null);
        }
    }
}